local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "agenthelper"
local msgproxy = require "msgproxy"
local playerdatadao = require "playerdatadao"
local processstate = require "processstate"
local table = table
require "enum"

local processing = processstate:new({timeout=4})
local  AgreeApplyDisband = {}

--[[
//同意或不同意申请解散
message AgreeAgreeApplyDisbandReq {
	optional Version version = 1;
	optional int32 id = 2;
	optional string roomsvr_id = 3; //房间服务器id
	optional int32  roomsvr_table_address = 4; //桌子的服务器地址
	optional int32  gameid = 5;
	optional int32 is_agree = 6; 0 表示否 1表示是		 		
}

message AgreeAgreeApplyDisbandRes {
	optional int32 errcode = 1;
	optional string errcodedes = 2;	
}
]]

function  AgreeApplyDisband.process(session, source, fd, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}
	local server = msghelper:get_server()

	--检查当前登陆状态
	if not msghelper:is_login_success() then
		filelog.sys_warning("AgreeApplyDisband.process invalid server state", server.state)
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求！"
		msghelper:send_resmsgto_client(fd, "AgreeApplyDisbandRes", responsemsg)		
		return
	end

	if processing:is_processing() then
		responsemsg.errcode = EErrCode.ERR_DEADING_LASTREQ
		responsemsg.errcodedes = "正在处理上一次请求！"
		msghelper:send_resmsgto_client(fd, "AgreeApplyDisbandRes", responsemsg)		
		return
	end

	if server.gameid ~= 0 
		and server.gameid ~= request.gameid then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前请求不符合当前游戏！"
		msghelper:send_resmsgto_client(fd, "AgreeApplyDisbandRes", responsemsg)		
		return
	end

	if server.roomsvr_id ~= "" 
		and server.roomsvr_table_id > 0 
		and server.roomsvr_table_id ~= request.id then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "当前请求不符合当前游戏！"
		msghelper:send_resmsgto_client(fd, "AgreeApplyDisbandRes", responsemsg)		
		return		
	end
	request.rid = server.rid
	request.gatesvr_id = skynet.getenv("svr_id")
	request.agent_address = skynet.self()
	
	processing:set_process_state(true)
	responsemsg = msgproxy.sendrpc_reqmsgto_roomsvrd(request.gameid, nil, request.roomsvr_id, request.roomsvr_table_address, "agreeapplydisband", request)
	processing:set_process_state(false)

	if not msghelper:is_login_success() then
		return
	end

	if responsemsg == nil then
		responsemsg = {
			errcode = EErrCode.ERR_INVALID_REQUEST,
			errcodedes = "无效的请求!",
		}
	end

	msghelper:send_resmsgto_client(fd, "AgreeApplyDisbandRes", responsemsg)
end

return AgreeApplyDisband

